define(function (require, exports, module) {
    return function (jQuery) {
        (function ($) {
            /*seajs-content-start*/
            var sparkStreamParaCtrl = function ($scope, httpservice){
                
                var isUserExist = false;
                $scope.visible = false;
                var getRoles = function(){
                    $("#role").html("");

                    var condition = {
                        nowpage:1,
                        pagesize:30
                    };
                    var getrole =  httpservice.javaPost("sysRoles.do?method=sysRoleGrid",condition, function (result) {

                        for(var i=0;i<result.datas.length;i++){
                            if(result.datas[i].id != 4 && result.datas[i].id != 5 && result.datas[i].id != 92 ){
                                /*$("#role").append("<option value='"+result.datas[i].id+",92'>"+result.datas[i].rolename+"</option>");*/
                                $("#role").append("<option >"+result.datas[i].rolename+"</option>");
                            }
                        }
                        $scope.$apply();
                    });

                };
                /*******************用户 start************************/
                var clear = function(){
                	
                    $("#loginCode").val("");
                    $("#psw").val("");
                    $("#state").val("");
                    $("#nickname").val("");
                    $("#department").val("");
                    $("#email").val("");
                    $("#mobile").val("");
                    $("#role").val("");
                    $("#errorinfo").html("");
                };

                var getCondition = function(e){
                    return {
                        loginId: (e==null) ? '':e,//编辑用户时使用
                        loginCode: $("#loginCode").val(),
                        psw:$("#psw").val(),
                        state: $("#state").val(),
                        nickname:$("#nickname").val(),
                        department:$("#department").val(),
                        email:$("#email").val(),
                        mobile:$("#mobile").val(),
                        role:$("#role").val()
                    }
                };

                var save = function (){
                    var condition = getCondition(null);

                    var saveUserinfo =  httpservice.javaPost("sysLogins.do?method=addorupdatelogin",condition, function (result) {

                        if(result == 0){
                            alert("新增成功");
                        }else{
                            alert("新增失败");
                        }
                    });
                    return saveUserinfo;
                };

                var update = function (e){
                    var condition = getCondition(e);

                    var updateUserinfo =  httpservice.javaPost("sysLogins.do?method=addorupdatelogin",condition, function (result) {

                        if(result == 0){
                            alert("更新成功");
                        }else{
                            alert("更新失败");
                        }
                    });
                    return updateUserinfo;
                };

                $scope.user = {
                    title: "新增用户",
                    show: function () { },
                    ok: function () { },
                    cancel: function () { }
                };


                /*******************用户 end************************/
                /*<a class='delete' href='javascript:' role-id='"+rowObject.id+"' role-name='"+rowObject.rolename+"'' title='删除'>*/
                var operate = function(cellvalue, options, rowObject){
                    var listprint = "<a class='viewuser' href='javascript:'loginId='"+rowObject.loginId+"' loginCode='"+rowObject.loginCode+"' psw='"+rowObject.psw+"' state='"+rowObject.state+"' nickname='"+rowObject.nickname+"' department='"+rowObject.department+"' email='"+rowObject.email+"' mobile='"+rowObject.mobile+"' role='"+rowObject.role+"' title = '查看'><img src='../../../images/chakan.png' /></a>" +
                        "<a class='editoruser' style='margin: 0 15px;' href='javascript:'loginId='"+rowObject.loginId+"' loginCode='"+rowObject.loginCode+"' psw='"+rowObject.psw+"' state='"+rowObject.state+"' nickname='"+rowObject.nickname+"' department='"+rowObject.department+"' email='"+rowObject.email+"' mobile='"+rowObject.mobile+"' role='"+rowObject.role+"'  title='编辑'><img src='../../../images/bianji.png' /></a>" +
                        "<a class='deleteuser' href='javascript:' loginId='"+rowObject.loginId+"' loginCode='"+rowObject.loginCode+"' title='删除'><img src='../../../images/delete1.png' /></a>";
                    return listprint;
                };

                $scope.grid1 = {
                    gridpagedata:{
                        nowpage:1,
                        totalpage:1,
                        totalcount:20,
                        startline:1,
                        endline:20,
                        pagesize:20,
                        datacount:20,
                        pagelist:[20,50,100],
                        toFirstpage: function(){
                            $scope.grid1.gridpagedata.nowpage = 1;
                            $scope.turnPage();
                        },
                        toToppage: function(){
                            if($scope.grid1.gridpagedata.nowpage>1){
                                $scope.grid1.gridpagedata.nowpage--;
                            }
                            $scope.turnPage();
                        },
                        toNextpage: function(){
                            if($scope.grid1.gridpagedata.nowpage<$scope.grid1.gridpagedata.totalpage){
                                $scope.grid1.gridpagedata.nowpage++;
                            }
                            $scope.turnPage();
                        },
                        toLastpage: function(){
                            $scope.grid1.gridpagedata.nowpage = $scope.grid1.gridpagedata.totalpage;
                            $scope.turnPage();
                        },
                        topage: function(){
                            if($scope.grid1.gridpagedata.nowpage<1){
                                $scope.grid1.gridpagedata.nowpage = 1;
                            }
                            if($scope.grid1.gridpagedata.nowpage>$scope.grid1.gridpagedata.totalpage){
                                $scope.grid1.gridpagedata.nowpage = $scope.grid1.gridpagedata.totalpage;
                            }
                            $scope.turnPage();
                        },
                        changePagesize: function(){
                            $scope.grid1.gridpagedata.nowpage = 1;
                            $scope.turnPage();
                        }
                    },
                    griddata: {
                        colnames: ['编号','功能',"功能编码",'参数名称','参数编码','参数值','参数类型',"操作"],
                        colmodel: [
                            { name: '', index: 'loginId', sortable: false, sorttype: 'number', align:"center"},
                            { name: 'loginCode', index: 'loginCode', sortable: false, align:"center"},
                            { name: 'psw', index: 'psw', sortable: false, align:"center",hidden:true},
                            { name: 'state', index: 'state', sortable: true, sorttype: 'number', align:"center"},
                            { name: 'nickname', index: 'nickname', sortable: true, sorttype: 'number', align:"center"},
                            { name: 'department', index: 'department', sortable: true, sorttype: 'number',align:"center"},
                            { name: 'email', index: 'email', sortable: true, sorttype: 'number', align:"center"},
                            { name: 'mobile', index: 'mobile', sortable: true, sorttype: 'number', align:"center"}

                        ],
                        data: [
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"},
//                            { loginId: "1",loginCode: "YH111",psw: "111", state: "", nickname:"张三", "department":"研发部", "email": "123456789@qq.com",mobile:"18812345678", role:"系统管理员"}
                        ]
                    },
                    obj:{},
                    shrinkToFit:true,
                    gridresize: function (grid) {
                        grid.setGridWidth($(window).width());
                        grid.setGridHeight(400);
                    }
                };


                var getWhereSql =function(){

                };

                var queryData = function(condition){
                    getRoles();

                    var f =  httpservice.javaPost("sysLogins.do?method=showgrid",condition, function (result) {
                        for(var i = 0; i < result.datas.length; i++){
                            if(result.datas[i].state == 0){
                                result.datas[i].state = "无效";
                            }else{
                                result.datas[i].state = "有效";
                            }
                        }
                        $scope.grid1.gridpagedata.totalcount = result.recordCount;
                        $scope.grid1.gridpagedata.totalpage = result.pageCount;
                        $scope.grid1.gridpagedata.startline =result.startLine;
                        $scope.grid1.gridpagedata.endline = result.endLine;
                        $scope.grid1.gridpagedata.datacount = result.dataCount;
                        $scope.grid1.griddata.data = result.datas;

                        $scope.$apply();
                        return f;
                    });

                    $scope.$emit('loading', 'true');

                    $.when(f).then(function () {

                    }).always(function () {
                            $scope.$emit('loading', 'false');
                            $scope.$apply();
                        });
                };

                $scope.turnPage = function(){
                    var pagecondition = {
                        loginCode:$("#queryName").val(),
                        pageindex:$scope.grid1.gridpagedata.nowpage,
                        pagesize:$scope.grid1.gridpagedata.pagesize,
                        ispage:true,
                        wheresql:getWhereSql()
                    };
                    queryData(pagecondition);
                };
                /*******************页面初始化 start************************/
//                var getdata = function (){
//                    $scope.$emit('loading', 'true');
//                    //getRoles();
//
//                    var condition = {
//                        funcName:$("#queryName1").val(),
//                        paraName:$("#queryName2").val(),
//                        paraCode:$("#queryName3").val(),
//                        pageindex:$scope.grid1.gridpagedata.nowpage,
//                        pagesize:$scope.grid1.gridpagedata.pagesize
//                    };
//
//                    var getUserinfo =  httpservice.javaPost("SpkPara/query",condition, function (result) {
//
//                        for(var i = 0; i < result.datas.length; i++){
//                            if(result.datas[i].state == 0){
//                                result.datas[i].state = "无效";
//                            }else{
//                                result.datas[i].state = "有效";
//                            }
//                        }
//                        $scope.grid1.gridpagedata.totalcount = result.recordCount;
//                        $scope.grid1.gridpagedata.totalpage = result.pageCount;
//                        $scope.grid1.gridpagedata.startline =result.startLine;
//                        $scope.grid1.gridpagedata.endline = result.endLine;
//                        $scope.grid1.gridpagedata.datacount = result.dataCount;
//                        $scope.grid1.griddata.data = result.datas;
//
//                        $scope.$apply();
//
//                        return getUserinfo;
//                    });
//
//                    $.when(getUserinfo).then(function () {
//                    }).always(function () {
//                            $scope.$emit('loading', 'false');
//                            $scope.$apply();
//                        });
//                };
                
                

                $scope.init = function(){
                    getdata();
                };

                $scope.init();
                /*******************页面初始化 end************************/
                //判断用户是否存在
                var checkUserExist = function(){
                    var condition={
                        loginCode:$("#loginCode").val()
                    };
                    var is = httpservice.javaPost("sysLogins.do?method=checkLoginCode",condition, function (result) {
                        if(result == 1){
                            $('#loginCode').focus();
                            $("#errorinfo").html("<font color=\"red\">用户名已存在！</font>");
                            isUserExist = true;
                        }else{
                            $("#errorinfo").html("");
                            isUserExist = false;
                        }
                    });

                };
                //用户输入 检查
                $('#loginCode').live("input",function(){
                    checkUserExist();
                });
                var getRow = function (e){
                    $("#role").val($(e).attr("role"));
                    $("#loginCode").val($(e).attr("loginCode"));
                    $("#psw").val($(e).attr("psw"));
                    $("#state").val($(e).attr("state"));
                    $("#nickname").val($(e).attr("nickname"));
                    $("#department").val($(e).attr("department"));
                    $("#email").val($(e).attr("email"));
                    $("#mobile").val($(e).attr("mobile"));
                };
                $scope.query = function(){
                    $scope.grid1.gridpagedata.nowpage = 1;
                    $scope.grid1.gridpagedata.pagesize = 20;
                    $scope.$apply();
                    getdata();
                };
                $scope.newUser = function (){
                    clear();
                    $scope.visible = true;
                    $scope.user.title = "新增用户";
                    $scope.user.show();
                    $scope.user.ok = function (){
                        if($("#loginCode").val() !=''){
                            if(!isUserExist){
                                if($("#psw").val() !=''){
                                    var f = save();
                                    $.when(f).then(function () {
                                    }).always(function () {
                                            getdata();
                                            clear();
                                        });
                                }else{
                                    $("#errorinfo").html("<font color=\"red\">密码不能为空</font>");
                                    return false;
                                }
                            } else{
                                $("#errorinfo").html("<font color=\"red\">用户名已存在</font>");
                                return false;
                            }
                        }else{
                            $("#errorinfo").html("<font color=\"red\">用户名不能为空</font>");
                            return false;
                        }

                    }
                };

                $(".viewuser").live("click",function(){
                    clear();
                    $scope.visible = false;
                    $scope.user.title = "用户信息";
                    $scope.user.ok = function (){};
                    getRow(this);
                    $scope.user.show();
                });

                $(".editoruser").live("click",function(){
                    clear();
                    $scope.visible = false;
                    $scope.user.title = "编辑用户";
                    getRow(this);
                    var loginId = $(this).attr("loginId");
                    $scope.user.show();
                    $scope.user.ok = function (){
                        if($("#loginCode").val() !=''){
                            if(!isUserExist){
                                if($("#psw").val() !=''){
                                    var f = update(loginId);
                                    $.when(f).then(function () {
                                    }).always(function () {
                                            getdata();
                                            clear();
                                        });
                                }else{
                                    $("#errorinfo").html("<font color=\"red\">密码不能为空</font>");
                                    return false;
                                }
                            } else{
                                $("#errorinfo").html("<font color=\"red\">用户名已存在</font>");
                                return false;
                            }
                        }else{
                            $("#errorinfo").html("<font color=\"red\">用户名不能为空</font>");
                            return false;
                        }

                    }
                });

                $(".deleteuser").live("click",function(){
                    var userid=$(this).attr('loginId');

                    var condition={
                        id:userid
                    };
                    if(confirm("确定删除 "+$(this).attr('loginCode')+" 用户")){
                        var deleterole=httpservice.javaPost("sysLogins.do?method=deletelogin",condition, function (result) {
                            if(result == 0){
                                alert("删除成功");
                            }else{
                                alert("删除失败");
                            }

                        });
                        $.when(deleterole).then(function () {
                        }).always(function () {
                                getdata();
                            });
                    }
                });
            };
            angular.module('sparkStreamPara-ctrl', [])
                .controller('SparkStreamParaCtrl', ['$scope', 'HttpService', sparkStreamParaCtrl])
            /*seajs-content-end*/
        })(jQuery);
    }
});